package com.supermap.dataservice.cloud.science.assets.comm;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.assets.comm.Comm;
import com.supermap.dubbo.api.cloud.assets.comm.CommService;
import io.swagger.models.auth.In;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2019年10月23 17:10
 * @email 1301584033@qq.com
 */
@Service
public class CommServiceImpl implements CommService {

    @Resource
    private CommMapper commMapper;

    @Override
    public JSONUtils getCommodityPageList(String name, Integer stype, Integer parenId, Integer page, Integer size) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(commMapper.getCommodityPageList(name,stype,parenId,(page-1)*size,size));
    }

    @Override
    public JSONUtils putCommodity(Integer stype, String name, Integer parenId,Double price, String unit, String conctent) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(commMapper.putCommodity(stype,name,parenId,price,unit,conctent));
    }

    @Override
    public JSONUtils delCommodity(Integer id) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(commMapper.delCommodity(id));
    }



    @Override
    public JSONUtils getCommodityCountList(Integer pId,Integer limit) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<Map<String,Object>> list=commMapper.getCommodityCountList(pId,limit);
        list.stream().forEach(map->{
            map.put("sNumList",(commMapper.getCommodityCountListConmm((Integer)map.get("id"),2)));
            map.put("yNumList",(commMapper.getCommodityCountListConmm((Integer)map.get("id"),4)));
        });
        return new JSONUtils().setData(list);
    }

    @Override
    public JSONUtils getCommodityUsePageList(String username, Integer state, Integer page, Integer size) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<Map<String,Object>> objlist=commMapper.getCommodityUsePageList(username,state,page,size);
        objlist.stream().forEach(map ->{  map.put("conmmdit",commMapper.getComList((Integer)map.get("id")));});
        return new JSONUtils().setCountSum(commMapper.getCommodityCountUsePageList(username,state)).setData(objlist);
    }

    @Override
    public JSONUtils insertComm(Comm comm) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        commMapper.putComm(comm);
        Integer ID=commMapper.getId();
        comm.getList().stream().forEach(commcom ->{
            commcom.setUseId(ID);
            commMapper.putCommUse(commcom);
        });
        return new JSONUtils();
    }

    @Override
    public JSONUtils commodityAudit(Integer id,String scontent,String suerId, Integer state) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        if(state==2||state==4){
            Map<String,Object> map=commMapper.getMap(id);
            Integer pid=(Integer) map.get("pid");
            Integer num=(Integer) map.get("num");
            Integer stype= commMapper.getStype(pid);
            if(stype==1){
                //一次性直接改流程
                List<Integer> integerList=commMapper.getKuCunId(pid,num);
                if(integerList.size()<num){
                    return new JSONUtils().setCode(100).setMsg("库存不足，无法借出使用");
                }
                commMapper.updateListConmm(integerList,2);
            }else{
                //一次性直接改流程
                List<Integer> integerList=commMapper.getKuCunId(pid,num);
                if(integerList.size()<num){
                    return new JSONUtils().setCode(100).setMsg("库存不足，无法借出");
                }
                commMapper.putListConmm(integerList,id);
                commMapper.updateListConmm(integerList,0);
            }
        }
        if(state==3){
            List<Integer> integerList=commMapper.getKuCunIdByUsrId(id);
            commMapper.updateListConmm(integerList,1);
        }


        commMapper.manageAudit(id,scontent,suerId,state);
        return new JSONUtils();
    }
}
